package cn.itcast.zt.java8.model.state;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * create by zhangtian105 on 2021/9/15
 */
public class OrderServiceImpl implements IOrderService{
    @Autowired
    private OrderStateFactory factory;

    @Override
    public void saveOrUpdateOrder(Order order) {
        //查找出原来的订单
        Order oldOrder = this.getById(order.getId());
        if (Objects.isNull(oldOrder)) {
            //新增
            this.save(order);
            if (Objects.nonNull(order.getStatus())) {
                //调用状态机
                factory.getState(order.getStatus()).handle(order,null);
            }
            return;
        }

        //修改
        OrderState orderState = Objects.isNull(order.getStatus()) ? factory.getState(oldOrder.getStatus()) : factory.getState(order.getStatus());
        orderState.handle(order,oldOrder);
    }

    @Override
    public Order getById(Long id) {
        return null;
    }

    @Override
    public void save(Order order) {

    }
}
